package com.compass.mobile.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.ActivitySpecialColumnBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.KEY_DOCTOR_ID
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.ArticleBean
import com.compass.mobile.presenters.LectureColumnPresenter
import com.compass.mobile.ui.viewModel.DoctorViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 专栏列表
 * Date:2024/6/12 9:34
 */
class SpecialColumnActivity : BaseVBActivity<ActivitySpecialColumnBinding>() {


    companion object {
        fun start(context: Context,doctorId:String?) {
            val intent = Intent(context, SpecialColumnActivity::class.java)
            intent.putExtra(KEY_DOCTOR_ID,doctorId)
            context.startActivity(intent)
        }
    }
    private var mPage = 1
    private val doctorViewModel: DoctorViewModel by viewModels()
    private var doctorId:String?=null
    private var articles: MutableList<ArticleBean> = mutableListOf()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    override fun initView(savedInstanceState: Bundle?) {
        doctorId=intent.getStringExtra(KEY_DOCTOR_ID)

        FocusViewUtils.scaleView(mBinding.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding.tvNextPage, 1)
        mBinding.caseItem.post {
            // 使用获取到的宽度
            val width = mBinding.caseItem.width
             specialColumn(width)
        }

    }

    override fun initData() {
        doctorViewModel.articleLiveData.observe(this) {
            it?.let { list ->
                articles = list
                if (articles.isNotEmpty()) {
                    mBinding.emptyView.gone()
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, articles)
                    mBinding.caseItem.requestFocus()
                } else {
                    mBinding.emptyView.visible()
                }
            }?:kotlin.run {
                mBinding.emptyView.visible()
            }
        }

    }


    override fun intListener() {
        mBinding.tvPrevious.onClick {
            showToast("上一页")
        }

        mBinding.tvNextPage.onClick {
            showToast("下一页")
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val  item =articles[position]
            VideoPlayActivity.start(this@SpecialColumnActivity,item.videoUrl)
        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn(width: Int) {
        mBinding.caseItem.setNumColumns(4)
        mBinding.caseItem.horizontalSpacing = dpToPx(10)
        mBinding.caseItem.verticalSpacing = dpToPx(10)
        val finalWidth = width - dpToPx(80)
        val lectureColumnPresenter =
            LectureColumnPresenter()
        lectureColumnPresenter.setFinalWidth(finalWidth / 4)
        arrayObjectAdapter = ArrayObjectAdapter(lectureColumnPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, onItemClickListener
        )
        mBinding.caseItem.setAdapter(itemBridgeAdapter)
        mBinding.caseItem.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, articles)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
        doctorViewModel.getArticleList(doctorId, "2", mPage, 12)
    }
}
